package com.yhg.member.controller;

import com.yhg.common.utils.R;
import com.yhg.member.entity.BlAdmin;
import com.yhg.member.entity.vo.LoginParamVo;
import com.yhg.member.service.BlAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author 凌月青缃
 * @date 2021/1/22
 */
@RequestMapping("/user")
@RestController
@CrossOrigin
public class LoginController {

    @Autowired
    BlAdminService adminService;

    @PostMapping("login")
    public R login(@RequestBody LoginParamVo vo){
        String token = adminService.login(vo);
        if (token!=null){
            return R.ok().data("token",token);
        }else
            return R.error();
    }

    @GetMapping("info")
    public R info(@RequestParam("token") String token){
        BlAdmin userInfo = adminService.getInfo(token);
        if (!StringUtils.isEmpty(userInfo)){
            return R.ok().data("roles","[管理员]").data("avatar",userInfo.getHeader()).data("name",userInfo.getUsername());
        }else return R.error();
    }

}
